class Solution {
public:
    int totalFruit(vector<int>& fruits) {
      int arr1[100001]={0};
      int right = 0;
      int left = 0;
      int len = 1;
      int count = 0;
      int arr2[100001] = {0};
      int kinds = 0;
      for(int i = 0;i<fruits.size();i++)
      {
        if(arr1[fruits[i]]==0) kinds++;
        arr1[fruits[i]]++;
      }
      while(right<fruits.size())
      {
         if(arr2[fruits[right]]==0) count++;
         arr2[fruits[right]]++;
         if(count==2||kinds==1)
         {
           len = max(right-left+1,len);
         }
         while(count>2)
         {
            arr2[fruits[left]]--;
            if(arr2[fruits[left]]==0) count--;
            left++;
         }
         right++;
      }
      return len;


    }
};
